package com.haoxin.ai_course.modules.course.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.haoxin.ai_course.common.annotation.SysLog;
import com.haoxin.ai_course.common.base.AbstractController;
import com.haoxin.ai_course.common.utils.HttpJsonResult;
import com.haoxin.ai_course.modules.course.biz.AcCourseUserBusiness;
import com.haoxin.ai_course.modules.course.entity.PO.AcCourseUserPO;
import com.haoxin.ai_course.modules.course.entity.VO.AcCourseUserVO;
import com.haoxin.ai_course.modules.course.service.AcCourseUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;



/**
 * 课程用户表
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app/course/accourseuser")
@Api(value="api",tags="app小程序-课程用户管理")
@Slf4j
public class AcCourseUserController  extends AbstractController {
    private final  AcCourseUserBusiness acCourseUserBusiness;
    /**
     * 全部列表
     * 功能改造，用户第一次进来，读取所有课程写入课程用户关联表，
     * 第一次读取第一课的状态是学习中，其他是未学习
     * 每学习一课更新一课的状态，通过changeStatus接口来修改状态
     * 
     */
    @GetMapping("/fullList")
    @ApiOperation("全部课程列表")
    public HttpJsonResult<List<AcCourseUserVO>> fullList(){
    	//查询列表数据
    	HttpJsonResult<List<AcCourseUserVO>>  result = new HttpJsonResult<List<AcCourseUserVO>> ();
    	try {
    		Long userId = getUserId();
    		if(userId == null) {
    			result.setMsg("用户未登录");
    			return result;
    		}
			result = acCourseUserBusiness.fullList(getUserId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return result;
    }


    /**
     * 修改
     */
    @SysLog("修改用户学习课程状态")
    @PostMapping("/changeStatus")
    @ApiOperation("修改用户学习课程状态")
    public HttpJsonResult<Boolean> changeStatus(@RequestBody AcCourseUserPO acCourseUserPO){
    	return acCourseUserBusiness.changeStatus(acCourseUserPO,getUser());
    }
    @ApiOperation("统计用户学习时长")
    @GetMapping("/updateCourseStudyTime")
    public HttpJsonResult<Boolean> updateCourseStudyTime(@ApiParam("课程id") int courseId,@ApiParam("环节id") int nodeId,@ApiParam("学习时长") int studyTime,@ApiParam("数据标识")String dataId,HttpServletRequest req){
    	Long userId = getUserId();
    	HttpJsonResult<Boolean>  result =  new HttpJsonResult<Boolean>();
    	try {
    		result = acCourseUserBusiness.updateCourseStudyTime(userId,courseId,nodeId,studyTime,getUser().getMobile(),dataId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("统计用户学习时长发生异常，错误信息："+e.getMessage());
			result.setMsg(e.getMessage());	
		}
    	return result;
    }
	
}
